
//
//  OAuthViewController.swift
//  Weibo
//
//  Created by 冯童童 on 16/1/12.
//  Copyright © 2016年 冯童童. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking

class OAuthViewController: UIViewController {

    let webView = UIWebView()
    
    
    @objc private func close() {
        
        dismissViewControllerAnimated(true, completion: nil)
        
    }
    
    
    @objc private  func defaultAccount(){
        
    
        let jsString = "document.getElementById('userId').value = 'lovlot126@126.com', document.getElementById('passwd').value ='td111672' "
        webView.stringByEvaluatingJavaScriptFromString(jsString)
        
        
        
    }
    
    override func loadView() {
        
        view = webView
        
        webView.delegate = self
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //navigationController?.navigationBar.tintColor = UIColor.orangeColor()
        
//        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        
        print(view)
        
        setNavBar()
    
        loadOAuthPage()
        
    }

    private func setNavBar() {
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain, target: self, action: "close")
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: .Plain, target: self, action: "defaultAccount")

        
        
    }
   
    private func loadOAuthPage() {
        
        let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
        
        let url = NSURL(string: urlString)!
        
        let request = NSURLRequest(URL: url)
        
        webView.loadRequest(request)
        
        
    }

    
}

extension OAuthViewController : UIWebViewDelegate {
    
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
        
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        
        SVProgressHUD.dismiss()

    }
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
       // print(request.URL?.absoluteString)
        
        guard let urlString = request.URL?.absoluteString else {
            return false
        }
        //print(urlString)
        
        if urlString.containsString("code="){
            print(urlString)
           guard let query = request.URL?.query else{
                
                return false
            }
            
            
            let codeStr = "code="
            
            let code = (query as NSString).substringFromIndex(codeStr.characters.count)
            print(code,query)
            
            loadAccessToken(code)
 
             //欢迎页面检测
            return false

        }
        
        return true
        
    }
    
    private func  loadAccessToken(code : String){
        
        UserAccountViewModel().loadAccessToken(code) { (isSuccess) -> () in
            
            if !isSuccess {
                print("用户登录失败")
                
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                return
                
            }
            
            
            SVProgressHUD.showSuccessWithStatus("登录成功")
            self.dismissViewControllerAnimated(false , completion: { () -> Void in
                NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVC, object: "Welcome")
            })
            
        }
    
        
    }
    
    
}




